/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.hibernate.spring;

import com.knezevic.realestate.Member;
import com.knezevic.realestate.beans.MemberBean;
import com.knezevic.realestate.service.Members;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Nemanja
 */
public class MembersDAO implements Members{

    private SessionFactory sessionFactory;

     public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Transactional
    public Member save(Member member) {
        Long id = (Long) sessionFactory.getCurrentSession().save(member);
        return (Member) sessionFactory.getCurrentSession().get(MemberBean.class, id);
    }

    @Transactional
    public void update(Member member) {
        sessionFactory.getCurrentSession().saveOrUpdate(member);
    }

    @Transactional
    public void deleteMember(Member member) {
        sessionFactory.getCurrentSession().delete(member);
    }

    @Transactional
    public Collection<Member> retrieve() {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MemberBean.class);
        return criteria.list();
    }

    @Transactional
    public Collection<Member> retrieveRegularMembers() {
        return null;
    }

    @Transactional
    public Member retrieve(Serializable id) {
        return (Member) sessionFactory.getCurrentSession().get(MemberBean.class, id);
    }

}
